import { ref } from 'vue';

import { ElMessage, ElMessageBox } from 'element-plus';

import { requestApi } from '#/api';
// 定义参数类型
type option = {
  [key: string]: any;
};
export default function useTableHooks(option: option) {
  const editRef = ref();
  // 获取列表
  async function fetchData(payload: any): Promise<void> {
    option.tableListLoading = true;
    // 后端枚举值类型不能为空 这里去掉空的值
    const keys = (payload && Object.keys(payload)) || [];
    keys.map((item: any) => {
      if (!payload[item] && typeof payload[item] !== 'number')
        delete payload[item];
    });
    const { data, code, allCount } = await requestApi(
      option.api.list.url,
      payload,
      option.api.list.method,
    );
    if (code === 2000) {
      option.tableList =
        (data && Array.isArray(data) && data) ||
        (data && data.records.length > 0 && data.records) ||
        [];
      option.total =
        (data && Array.isArray(data) && Number(allCount)) ||
        0 ||
        (data && data.total) ||
        0;
    }
    option.tableListLoading = false;
  }
  // 修改搜索参数
  async function changeSearch(ser: any) {
    option.queryForm = ser;
    await fetchData(option.queryForm);
  }
  // 显示新建编辑页
  function handleEdit(data: any, option: any) {
    editRef.value.showEdit(data, option);
  }
  // 删除
  async function handleDelete(data: any) {
    if (data && Array.isArray(data)) {
      if (data.length > 0) {
        const ids = data.map((item) => item.id).join(',');
        ElMessageBox.confirm('确定要删除选中项吗', '', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(async () => {
            await requestApi(
              option.api.delete.url,
              ids,
              option.api.delete.method,
            );
            await fetchData(option.queryForm);
            ElMessage({
              type: 'success',
              message: '删除成功',
            });
          })
          .catch(async () => {
            await fetchData(option.queryForm);
          });
      } else {
        ElMessage({
          type: 'error',
          message: '未选中任何行',
        });
      }
    } else if (typeof data === 'object' && data.id) {
      ElMessageBox.confirm('确定要删除选中项吗', '', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          await requestApi(
            option.api.delete.url,
            { id: data.id, delFlag: 1 },
            option.api.delete.method,
          );
          await fetchData(option.queryForm);
          ElMessage({
            type: 'success',
            message: '删除成功',
          });
        })
        .catch(async () => {
          await fetchData(option.queryForm);
        });
    }
  }
  // 更新状态
  async function updateStatus(row: any, type: string, key: string = 'status') {
    await requestApi(
      option.api.updateStatus.url,
      {
        id: row.id,
        [key]: type,
      },
      option.api.updateStatus.method,
    );
    await fetchData(option.queryForm);
    ElMessage({
      type: 'success',
      message: `${type === 'ENABLE' ? '启用' : '禁用'}成功`,
    });
  }
  return {
    editRef,
    fetchData,
    changeSearch,
    handleEdit,
    handleDelete,
    updateStatus,
  };
}
